/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.UserBean;
import com.minierp.dao.EnterpriseInfoDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.EnterpriseInfo;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class EnterpriseInfoFormCtr extends SimpleFormController {
	private EnterpriseInfoDao enterpriseInfoDao;

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "SYST_CONF")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			EnterpriseInfo ei = this.enterpriseInfoDao.get();
			return ei;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		EnterpriseInfo obj = (EnterpriseInfo) command;
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		String uploadPath = EncCommonHelper.getProperty("uploadVirtualPath");
		String realPath = this.getServletContext().getRealPath(uploadPath)
				+ "/";
		File file = new File(realPath);
		if (!file.isDirectory()) {
			file.mkdir();
		}

		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = multipartRequest.getFile("logo_image");
		String alertMsg;
		String extendName;
		String fileName;
		if (multipartFile != null && !multipartFile.isEmpty()) {
			alertMsg = multipartFile.getOriginalFilename();
			extendName = EncCommonHelper.getExtendName(alertMsg);
			fileName = ub.getUser().getId() + "_" + System.currentTimeMillis()
					+ "." + extendName;
			FileCopyUtils.copy(multipartFile.getBytes(), new File(realPath
					+ System.getProperty("file.separator") + fileName));
			obj.setImage(uploadPath + fileName);
		}

		multipartFile = multipartRequest.getFile("login_logo");
		if (multipartFile != null && !multipartFile.isEmpty()) {
			alertMsg = multipartFile.getOriginalFilename();
			extendName = EncCommonHelper.getExtendName(alertMsg);
			fileName = ub.getUser().getId() + "_" + System.currentTimeMillis()
					+ "login." + extendName;
			FileCopyUtils.copy(multipartFile.getBytes(), new File(realPath
					+ System.getProperty("file.separator") + fileName));
			obj.setLoginImage(uploadPath + fileName);
		}

		alertMsg = "";
		this.enterpriseInfoDao.update(obj);
		EncCommonHelper.setEnterpriseInfo(request, obj);
		alertMsg = "修改成功！";
		EncCommonHelper.setAlertMessage(request, alertMsg);
		return this.showForm(request, errors, this.getFormView());
	}

	public EnterpriseInfoDao getEnterpriseInfoDao() {
		return this.enterpriseInfoDao;
	}

	public void setEnterpriseInfoDao(EnterpriseInfoDao enterpriseInfoDao) {
		this.enterpriseInfoDao = enterpriseInfoDao;
	}
}